class Project < ActiveRecord::Base
  # Relations
  has_many  :teachings,
            :dependent => :destroy
  # Validators
  validates_presence_of   :title
  validates_uniqueness_of :title
  
  # Returns wether or not a user can join a project.
  # It can't if she's already in a class for the project or if she's a teacher.
  #
  def can_join?(user)
    user = User.find(user) unless user.is_a?(User)
    return false if user.is_teacher?
    return false if self.teachings.find(:first, :conditions => ['users.id = ?', user.id], :include => [{:groups => :users}])
    return true
  end
  
  # Returns active classes
  #
  def active_teachings
    self.teachings.find(:all, :conditions => ['end > ?', Time.now])
  end
  
  # Returns old classes
  #
  def old_teachings
    self.teachings.find(:all, :conditions => ['end <= ?', Time.now])
  end
end
